from selenium import webdriver
import time
import os
from selenium.webdriver.support.ui import Select


class Case:
    def __init__(self):
        self.driver = webdriver.Chrome()
        url = os.path.join(os.path.abspath('注册页面'), '注册实例.html')
        self.driver.get(url)

    def register(self):
        # 输入操作 点击注册
        user = self.driver.find_element_by_id('user')
        user.send_keys('zhangsan')

        password = self.driver.find_element_by_id('password')
        password.send_keys('zhangsan')

        tel = self.driver.find_element_by_id('tel')
        tel.send_keys('15189898989')

        email = self.driver.find_element_by_id('email')
        email.send_keys('45679@qq.com')

        time.sleep(3)
        register_element = self.driver.find_element_by_xpath('//button[@value="注册"]')
        register_element.click()

    def sina(self):
        self.driver.find_element_by_id('fw').click()
        sina_handles = self.driver.window_handles[1]  # 获取句柄
        self.driver.switch_to.window(sina_handles)  # 切换窗口

        time.sleep(1)

        self.driver.close()

    def select_element(self):
        time.sleep(2)
        self.driver.find_element_by_xpath('//option[@value="cq"]').click()
        time.sleep(2)

        select_dir = self.driver.find_element_by_id('select')
        select = Select(select_dir)
        select.select_by_visible_text('上海')

        self.driver.find_element_by_xpath('//option[contains(text(), "广州")]').click()  # 通过文本 contains

        xj = self.driver.find_element_by_id('xj')
        xj.click()
        print(xj.is_selected())

        checkbox_list = self.driver.find_elements_by_xpath('//input[@name="checkbox"]')
        # checkbox_list[1].click()
        checkbox_list[2].click()
        time.sleep(2)
        for checkbox in checkbox_list:
            if not checkbox.is_selected():
                checkbox.click()
                time.sleep(2)

    def alert(self):
        self.driver.find_element_by_xpath('//input[@value="alert"]').click()
        alert = self.driver.switch_to.alert
        time.sleep(2)
        alert.accept()

    def file(self):
        file_cat = os.path.join(os.path.abspath('images'), "cat.webp")
        self.driver.find_element_by_xpath('//input[@name="upfile"]').send_keys(file_cat)

    def iframeA(self):

        iframeA = self.driver.find_element_by_xpath('//iframe[@frameborder="1"]')
        self.driver.switch_to.frame(iframeA)
        user_A = self.driver.find_element_by_id('userA')
        user_A.send_keys('qwqwerqwe')

    def iframeB(self):
        iframeB = self.driver.find_element_by_xpath('//iframe[@name="myframe2"]')
        self.driver.switch_to.frame(iframeB)
        user_B = self.driver.find_element_by_xpath('//*[@id="telB"]')
        user_B.send_keys('qwqwerqwe')
if __name__ == '__main__':
    case = Case()
    # case.register()
    # case.sina()
    # case.select_element()
    # case.alert()
    # case.file()
    # case.iframeA()
    case.iframeB()
